/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LayananKeluarForm.java
 *
 * Created on Nov 13, 2011, 9:54:37 PM
 */
package Com.TransaksiParkirdanLaporan.Com.Transaksi;

import Login.Karyawan;
import Login.LoginController;
import Login.MainForm;
import java.io.IOException;
import java.sql.SQLException;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import org.apache.commons.beanutils.converters.DateConverter;

/**
 *
 * @author Adrian
 */
public class LayananKeluarForm extends javax.swing.JFrame {

    /** Creates new form LayananKeluarForm */
    String tanggalKeluar;
    String jamKeluar;
    String idParkir;
    public String jamMasuk;

    public LayananKeluarForm(String jamMasuk) {
        initComponents();
        textField_NoPolisi1.requestFocus();
        this.jamMasuk = jamMasuk;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel8 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        panel_Header = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        backButton = new javax.swing.JButton();
        textField_DataHelm = new javax.swing.JTextField();
        textField_NoPolisi2 = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        button_Submit = new javax.swing.JButton();
        textField_NoPolisi3 = new javax.swing.JTextField();
        textField_Pembayaran = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        textField_TotalKembalian = new javax.swing.JTextField();
        button_Cancel = new javax.swing.JButton();
        textField_NoPolisi1 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        textField_WaktuKeluar = new javax.swing.JTextField();
        textField_WaktuMasuk = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        logoutMenuItem = new javax.swing.JMenuItem();

        jLabel8.setText("jLabel8");

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Layanan Keluar Form");
        setBackground(new java.awt.Color(255, 51, 51));
        setMinimumSize(new java.awt.Dimension(763, 400));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        panel_Header.setBackground(new java.awt.Color(255, 255, 255));
        panel_Header.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        panel_Header.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Com/TransaksiParkirdanLaporan/Com/Transaksi/parkir keluar.jpg"))); // NOI18N
        jLabel9.setText("jLabel9");
        panel_Header.add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, -1));

        jPanel1.add(panel_Header, new org.netbeans.lib.awtextra.AbsoluteConstraints(15, 31, 490, 80));

        backButton.setText("Back");
        backButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButtonActionPerformed(evt);
            }
        });
        jPanel1.add(backButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(15, 319, -1, -1));

        textField_DataHelm.setEditable(false);
        textField_DataHelm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textField_DataHelmActionPerformed(evt);
            }
        });
        jPanel1.add(textField_DataHelm, new org.netbeans.lib.awtextra.AbsoluteConstraints(575, 279, 150, -1));

        textField_NoPolisi2.setFont(new java.awt.Font("Tahoma", 1, 18));
        textField_NoPolisi2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textField_NoPolisi2ActionPerformed(evt);
            }
        });
        jPanel1.add(textField_NoPolisi2, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 140, 70, 38));

        jLabel7.setText("Data Helm");
        jPanel1.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(485, 279, -1, -1));

        button_Submit.setText("Submit");
        button_Submit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_SubmitActionPerformed(evt);
            }
        });
        jPanel1.add(button_Submit, new org.netbeans.lib.awtextra.AbsoluteConstraints(655, 319, -1, 30));

        textField_NoPolisi3.setFont(new java.awt.Font("Tahoma", 1, 18));
        textField_NoPolisi3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textField_NoPolisi3ActionPerformed(evt);
            }
        });
        textField_NoPolisi3.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                textField_NoPolisi3KeyTyped(evt);
            }
        });
        jPanel1.add(textField_NoPolisi3, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 140, 70, 38));

        textField_Pembayaran.setEditable(false);
        textField_Pembayaran.setFont(new java.awt.Font("Tahoma", 1, 18));
        textField_Pembayaran.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textField_PembayaranActionPerformed(evt);
            }
        });
        jPanel1.add(textField_Pembayaran, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 250, 230, 42));

        jLabel5.setText("Total Kembalian");
        jPanel1.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(485, 249, -1, -1));

        jLabel2.setText("UANG PEMBAYARAN");
        jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 220, -1, -1));

        jLabel4.setText("Waktu Keluar");
        jPanel1.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(485, 219, -1, -1));

        textField_TotalKembalian.setEditable(false);
        textField_TotalKembalian.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textField_TotalKembalianActionPerformed(evt);
            }
        });
        jPanel1.add(textField_TotalKembalian, new org.netbeans.lib.awtextra.AbsoluteConstraints(575, 249, 150, -1));

        button_Cancel.setText("Cancel");
        button_Cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_CancelActionPerformed(evt);
            }
        });
        jPanel1.add(button_Cancel, new org.netbeans.lib.awtextra.AbsoluteConstraints(485, 319, 73, 30));

        textField_NoPolisi1.setFont(new java.awt.Font("Tahoma", 1, 18));
        textField_NoPolisi1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        textField_NoPolisi1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textField_NoPolisi1ActionPerformed(evt);
            }
        });
        textField_NoPolisi1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                textField_NoPolisi1KeyTyped(evt);
            }
        });
        jPanel1.add(textField_NoPolisi1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 140, 70, 38));

        jLabel3.setText("Waktu Masuk");
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(485, 189, -1, -1));

        jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Com/TransaksiParkirdanLaporan/Com/Transaksi/secure-parking-logo-d.png"))); // NOI18N
        jPanel1.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(520, 30, -1, -1));

        textField_WaktuKeluar.setEditable(false);
        jPanel1.add(textField_WaktuKeluar, new org.netbeans.lib.awtextra.AbsoluteConstraints(575, 219, 150, -1));

        textField_WaktuMasuk.setEditable(false);
        jPanel1.add(textField_WaktuMasuk, new org.netbeans.lib.awtextra.AbsoluteConstraints(575, 189, 150, -1));

        jLabel1.setText("NO POLISI");
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 120, -1, -1));

        jMenu1.setText("Pindah Halaman");

        logoutMenuItem.setText("Logout");
        logoutMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                logoutMenuItemActionPerformed1(evt);
            }
        });
        jMenu1.add(logoutMenuItem);

        jMenuBar1.add(jMenu1);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 754, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 379, Short.MAX_VALUE)
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-741)/2, (screenSize.height-438)/2, 741, 438);
    }// </editor-fold>//GEN-END:initComponents

    private void textField_NoPolisi3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textField_NoPolisi3ActionPerformed
        try {
            // TODO add your handling code here:

            DataParkir dataParkir = new DataParkir();
            dataParkir.setKode_wilayah(textField_NoPolisi3.getText());
            textField_NoPolisi3.setText(textField_NoPolisi3.getText().toUpperCase());

            String noPolisi1 = textField_NoPolisi1.getText().trim();
            String noPolisi2 = textField_NoPolisi2.getText().trim();
            String noPolisi3 = textField_NoPolisi3.getText().trim();
            textField_NoPolisi1.setText(textField_NoPolisi1.getText().toUpperCase());
            if (noPolisi1.matches("") || noPolisi2.matches("") || noPolisi3.matches("")) {
                JOptionPane.showMessageDialog(null, "data no polisi harus disikan lengkap");
            } else {

                //    DataParkir dataParkir = new DataParkir();
                String noPolisi = noPolisi1.toUpperCase() + " " + noPolisi2 + " " + noPolisi3.toUpperCase();
                dataParkir.setNoPolisiKendaraan(noPolisi);
                TransaksiParkirController.cariDataParkir(dataParkir, textField_DataHelm,
                        textField_WaktuMasuk, textField_WaktuKeluar, textField_Pembayaran, textField_NoPolisi1, textField_NoPolisi2, textField_NoPolisi3);
                idParkir = dataParkir.getNoParkir();
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Kode wilayah harus berupa 1 hingga 3 karakter huruf");
        }
    }//GEN-LAST:event_textField_NoPolisi3ActionPerformed

    private void textField_TotalKembalianActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textField_TotalKembalianActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_textField_TotalKembalianActionPerformed

    private void textField_NoPolisi1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textField_NoPolisi1ActionPerformed
        try {
            // TODO add your handling code here:
            DataParkir dataParkir = new DataParkir();
            dataParkir.setKode_karesidenan(textField_NoPolisi1.getText());
            textField_NoPolisi1.setText(textField_NoPolisi1.getText().toUpperCase());
            textField_NoPolisi2.requestFocus();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Kode karesidenan harus berupa 1 atau 2 digit karakter huruf");
        }
    }//GEN-LAST:event_textField_NoPolisi1ActionPerformed

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
//        try {
//            // TODO add your handling code here:
//            RekapForm rekapForm = new RekapForm();
//            this.setVisible(false);
//            rekapForm.setVisible(true);
//        } catch (IOException ex) {
//            Logger.getLogger(LayananKeluarForm.class.getName()).log(Level.SEVERE, null, ex);
//        } catch (SQLException ex) {
//            Logger.getLogger(LayananKeluarForm.class.getName()).log(Level.SEVERE, null, ex);
//        }
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void textField_DataHelmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textField_DataHelmActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_textField_DataHelmActionPerformed

    private void button_SubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_SubmitActionPerformed
        // TODO add your handling code here:
        String noPolisi1 = textField_NoPolisi1.getText().trim();
        String noPolisi2 = textField_NoPolisi2.getText().trim();
        String noPolisi3 = textField_NoPolisi3.getText().trim();
        if (noPolisi1.matches("") || noPolisi2.matches("") || noPolisi3.matches("")) {
            JOptionPane.showMessageDialog(null, "data no kendaraan belum diisi");
        } else {
            DataParkir dp = new DataParkir();
            GregorianCalendar gc = new GregorianCalendar();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy,hh:mm:ss");
            SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("dd-MMM-yyyy");
            String vTanggalKeluar = simpleDateFormat1.format(gc.getTime());

            String jamTanggal = String.valueOf(simpleDateFormat.format(gc.getTime()));
            String[] jamTanggalBagi = jamTanggal.split(",");
            dp.setTanggalKeluar(vTanggalKeluar);
            dp.setJamKeluar(jamTanggalBagi[1]);
            dp.setNoParkir(idParkir);
            TransaksiParkirController.parkirKeluar(dp, this, button_Cancel, textField_Pembayaran);
        }
    }//GEN-LAST:event_button_SubmitActionPerformed

    private void textField_NoPolisi2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textField_NoPolisi2ActionPerformed
        try {
            // TODO add your handling code here:
            DataParkir dataParkir = new DataParkir();
            dataParkir.setNo_polisi(textField_NoPolisi2.getText());
            textField_NoPolisi3.requestFocus();
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "no polisi harus berupa 4 digit angka");
        }
    }//GEN-LAST:event_textField_NoPolisi2ActionPerformed

    private void textField_PembayaranActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textField_PembayaranActionPerformed
        // TODO add your handling code here:
        String bayar = textField_Pembayaran.getText();

        if (bayar.matches("")) {
            JOptionPane.showMessageDialog(rootPane, "Field pembayaran belum diisi");
        } else if(bayar.matches("[0-9]*")){
            int pembayaran = Integer.parseInt(bayar);
            if (pembayaran > 2000) {
                int kembalian = pembayaran - 2000;
                NumberFormat n = NumberFormat.getCurrencyInstance();
                textField_TotalKembalian.setText(n.format(kembalian));
            } else if (pembayaran >= 0 && pembayaran < 2000) {
                JOptionPane.showMessageDialog(rootPane, "Uang pembayaran kurang");
            } else {
                textField_TotalKembalian.setText("Rp0,00");
            }
        }else{
            JOptionPane.showMessageDialog(rootPane, "Field pembayaran hanya dapat diisi dengan angka");
        }
    }//GEN-LAST:event_textField_PembayaranActionPerformed

    private void button_CancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_CancelActionPerformed
        // TODO add your handling code here:
        textField_DataHelm.setText("");
        textField_NoPolisi1.setText("");
        textField_NoPolisi2.setText("");
        textField_NoPolisi3.setText("");
        textField_Pembayaran.setText("");
        textField_TotalKembalian.setText("");
        textField_WaktuKeluar.setText("");
        textField_WaktuMasuk.setText("");
        textField_Pembayaran.setEditable(false);
        textField_NoPolisi1.setEditable(true);
        textField_NoPolisi2.setEditable(true);
        textField_NoPolisi3.setEditable(true);
        textField_NoPolisi1.requestFocus();
    }//GEN-LAST:event_button_CancelActionPerformed

    private void logoutMenuItemActionPerformed1(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_logoutMenuItemActionPerformed1
        // TODO add your handling code here:
        GregorianCalendar jamTekanLogout = new GregorianCalendar();
        SimpleDateFormat dateFormat2 = new SimpleDateFormat("hh:mm:ss");
        String time = dateFormat2.format(jamTekanLogout.getTime());

        int hoursOut = Integer.parseInt(time.substring(0, 2));
        int minutesOut = Integer.parseInt(time.substring(3, 5));
        int secondsOut = Integer.parseInt(time.substring(6, 8));

        int hoursIn = Integer.parseInt(jamMasuk.substring(0, 2));
        int minutesIn = Integer.parseInt(jamMasuk.substring(3, 5));
        int secondsIn = Integer.parseInt(jamMasuk.substring(6, 8));

        int selisihWaktu = ((hoursOut - hoursIn) * 60 * 60) + ((minutesOut - minutesIn) * 60) + (secondsOut - secondsIn);

        if (selisihWaktu >= 15) {
            try {
                // TODO add your handling code here:
                Karyawan.logout();
                this.setVisible(false);
                LoginController.openLoginForm();
            } catch (Exception ex) {
                Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            JOptionPane.showMessageDialog(null, "Maaf waktu shift Anda belum selesai");
        }
    }//GEN-LAST:event_logoutMenuItemActionPerformed1

    private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backButtonActionPerformed
        // TODO add your handling code here:
        TransaksiParkirController.openMainForm2(this);
    }//GEN-LAST:event_backButtonActionPerformed

    private void textField_NoPolisi1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textField_NoPolisi1KeyTyped
        // TODO add your handling code here:
        if (Character.isDigit(evt.getKeyChar())) {
            evt.consume();
        }
    }//GEN-LAST:event_textField_NoPolisi1KeyTyped

    private void textField_NoPolisi3KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textField_NoPolisi3KeyTyped
        // TODO add your handling code here:
        if (Character.isDigit(evt.getKeyChar())) {
            evt.consume();
        }
    }//GEN-LAST:event_textField_NoPolisi3KeyTyped
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton backButton;
    private javax.swing.JButton button_Cancel;
    private javax.swing.JButton button_Submit;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JMenuItem logoutMenuItem;
    private javax.swing.JPanel panel_Header;
    private javax.swing.JTextField textField_DataHelm;
    private javax.swing.JTextField textField_NoPolisi1;
    private javax.swing.JTextField textField_NoPolisi2;
    private javax.swing.JTextField textField_NoPolisi3;
    private javax.swing.JTextField textField_Pembayaran;
    private javax.swing.JTextField textField_TotalKembalian;
    private javax.swing.JTextField textField_WaktuKeluar;
    private javax.swing.JTextField textField_WaktuMasuk;
    // End of variables declaration//GEN-END:variables
}
